home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / mnpc12.zip / MNPDRVR.C < prev    next >
C/C++ Source or Header  |  1987-11-16  |  22KB  |  888 lines

  1. /*=====================================================================
  2.  
  3.                       The Microcom MNP Library
  4.                         (Microsoft C Version)
  5.  
  6.  
  7.     11/4/87 - explicit data typing...gp
  8.     8/27/87 - rcv_lt() modified to use lcb.lcl_credit..gp
  9.  
  10. -----------------------------------------------------------------------
  11.  
  12.         LDRIVER - MNP Link RS-232 Interrupt Service Routines
  13.  
  14.      This module implements asynchronous I/O for IBM PC-compatible
  15.      machines and thus much of this code may not be portable to 
  16.      other dissimilar environments.
  17.  
  18.      This code implements Class 2 of the MNP link protocol,
  19.      i.e. it uses the stop/start byte-oriented framing technique.
  20.      A Class 3 implementation is not possible in the IBM PC 
  21.      environment since the communications hardware is not capable
  22.      of switching to synchronous (bit-oriented) operation.
  23.      Protocol messages and procedures, however, are independent
  24.      of the framining technique.        
  25.  
  26.      Global routines:
  27.  
  28.     - ascode: service I/O interrupt 
  29.     - lne_stat: check carrier status 
  30.     - trigger_sf: initiate transmit interrupts
  31.  
  32. =====================================================================*/
  33.  
  34. /* Header files
  35. */
  36. #include <dos.h>
  37. #include <mnpdat.h>
  38.  
  39. /* Interrupt-related definitions
  40. */
  41. #define INT_8259A_PORT    0x20
  42. #define EOI        0x20
  43. #define RCV_DATA    4
  44. #define THR_EMPTY    2
  45. #define MODSTAT_CHNG    0
  46.  
  47. /* Send framer state names
  48. */
  49. #define SF_INIT     0
  50. #define SF_DLE        1
  51. #define SF_STX        2
  52. #define SF_INFO     3
  53. #define SF_DLEINFO    4
  54. #define SF_ETX        5
  55. #define SF_FCS1     6
  56. #define SF_FCS2     7
  57.  
  58. /* Receive framer state names
  59. */
  60. #define RF_INIT     0
  61. #define RF_DLE        1
  62. #define RF_STX        2
  63. #define RF_HDLEN    3
  64. #define RF_HEADER    4
  65. #define RF_INFOL    5
  66. #define RF_INFO     6
  67. #define RF_FCS1     7
  68. #define RF_FCS2     8
  69.  
  70. /* Frame octet definitions
  71. */
  72. #define SYN        0x16
  73. #define DLE        0x10
  74. #define ETX        3
  75. #define STX        2
  76.  
  77. /* Length of header buffer (maximum acceptable LPDU header)
  78. */
  79. #define MAX_HDRLEN    100
  80.  
  81. /* Bit set, clear and test definitions
  82. */
  83. #define SETBIT1(bit)    lcb.status_1 |= bit;       
  84. #define SETBIT2(bit)    lcb.status_2 |= bit;
  85. #define SETBIT3(bit)    lcb.status_3 |= bit;
  86. #define CLRBIT1(bit)    lcb.status_1 &= ~bit;         
  87. #define CLRBIT2(bit)    lcb.status_2 &= ~bit;
  88. #define CLRBIT3(bit)    lcb.status_3 &= ~bit;
  89. #define BIT1SET(bit)    (lcb.status_1 & bit)         
  90. #define BIT2SET(bit)    (lcb.status_2 & bit)
  91. #define BIT3SET(bit)    (lcb.status_3 & bit)
  92.  
  93. /* External references
  94. */
  95. extern USIGN_16 iir_add;            /* int id reg address */
  96. extern USIGN_8 linestat;        /* line status var */
  97. extern USIGN_16 fcw_tm;            /* window timer */
  98. extern struct link_ctl_blk lcb;  /* link control block */
  99. extern struct BUFFER rb, ftb, rlkb;  /* buffers */
  100. extern USIGN_8 *rb_iptr;        /* rcv buf insert pointer */
  101. extern USIGN_16 rb_cnt,tbcnt;        /* buffer counts */
  102. extern USIGN_8 rbuf[RBUF_LEN];    /* receive buffer */
  103. extern USIGN_16 ln_tm;
  104.  
  105. /* Function definitions
  106. */
  107. void snd_framer();
  108. void rcv_framer();
  109. void mod_stat();
  110. void rcv_lt();
  111. void rcv_la();
  112. void rcv_lna();
  113. void rcv_ln();
  114.  
  115. /* Local data
  116. */
  117. USIGN_8 *sf_ptr,
  118.      *rf_hptr,
  119.      *rf_dptr;
  120. USIGN_8 snd_char,
  121.      rcv_char;
  122. struct 
  123.     {
  124.     USIGN_8 low;
  125.     USIGN_8 hi;
  126.     }
  127.         snd_fcs,            /* send fcs */
  128.           rcv_fcs,            /* calculated receive fcs */
  129.           rfcs;            /* received fcs */
  130. USIGN_16 rdle_flg;        /* 'previous char DLE' flag */
  131. USIGN_16 rf_hdrlen,                /* receive header length  */
  132.     hdrcnt,            
  133.     rf_datlen,
  134.     sf_state,                /* send framer state var */
  135.     rf_state,                /* receive framer state var */
  136.     sf_len,
  137.     sf_busy,                /* 'send frame' flag */
  138.     sf_lt,                /* 'sending LT' flag */
  139.     frame_snt,                /* 'info field sent' flag */ 
  140.     frame_rcvd,                /* 'frame received' flag */
  141.     frame_dne;                /* 'frame sent' flag */
  142. USIGN_8 *rf_tiptr;
  143.  
  144. struct MNP_CB *p_mnpcb;
  145. struct BLST *dat_struct, *hdr_struct;
  146.  
  147. USIGN_16 modem_out_busy;    /* RS-232 hardware status flag
  148.                               0=transmitter not active
  149.                               1=transmitter active */
  150.  
  151. /* Function declarations
  152. */
  153. USIGN_8 get_char();
  154.  
  155. /*GLOBAL***************************************************************
  156.  
  157.     ascode - MNP RS-232 interrupt service routine
  158.  
  159.      This routine is called by 'async' (in module 'async.asm')
  160.      to service an async I/O interrupt.
  161.  
  162. **********************************************************************/
  163.  
  164. ascode()
  165. {
  166.  
  167. USIGN_8 int_id_reg;            /* value in int id reg */
  168.  
  169. /* Handle interrupts for as long as there are any. 
  170. */
  171. for (;;)
  172.     {
  173.  
  174. /* Read the Interrupt Identification Register.  If bit 0 is on, 
  175. ** there is no interrupt.  In this case cancel the interrupt by writing ** the "end-of-interrupt" byte to the Oper Ctl Word 2 and return. 
  176. */
  177.     if ((int_id_reg = inp(iir_add)) & BIT0_ON)
  178.         {
  179.         outp(INT_8259A_PORT, EOI);
  180.         return;
  181.         }
  182.  
  183. /* Since bit 0 is off, there is an interrupt.  Take action based
  184. ** on the type of interrupt. 
  185. */
  186.     switch (int_id_reg)
  187.         {
  188.  
  189. /* Receive buffer full interrupt
  190. */
  191.         case RCV_DATA:                
  192.             rcv_framer();
  193.             break;
  194.  
  195. /* Transmit buffer empty interrupt
  196. */
  197.         case THR_EMPTY:
  198.             snd_framer();
  199.             break;
  200.  
  201. /* Modem status change interrupt
  202. */
  203.         case MODSTAT_CHNG:            
  204.             mod_stat();
  205.             break;
  206.         }
  207.     }
  208. }
  209.  
  210. /*GLOBAL***************************************************************
  211.  
  212.     lne_stat - physical-connection status routine
  213.  
  214. **********************************************************************/
  215.  
  216. SIGN_16 lne_stat()
  217. {
  218.  
  219. /* If the carrier detect bit is 1, return success (physical-connection
  220. ** still active).  Otherwise, return with a failure indication. 
  221. */
  222. if (linestat & 0x80)
  223.     return (SUCCESS);
  224. else
  225.     return (1);
  226. }
  227.  
  228. /*GLOBAL***************************************************************
  229.  
  230.     trigger_sf - initiate send framer
  231.  
  232. **********************************************************************/
  233.  
  234. void trigger_sf()
  235. {
  236.  
  237. extern USIGN_16 port_add;
  238.  
  239. /* If the RS-232 transmitter is not already busy, then a byte must 
  240. ** be placed in the transmit buffer to begin interrupt-driven sending.
  241. ** This is the start of a frame and thus the first byte to be sent is
  242. ** the SYN which begins the frame start flag.  Also the send framer's
  243. ** state variable is initialed here.
  244. */ 
  245. if (!modem_out_busy)
  246.     {
  247.     modem_out_busy = 1;
  248.     sf_state = SF_DLE;                
  249.     outp(port_add, SYN);
  250.     }
  251. }
  252.  
  253. /*LOCAL----------------------------------------------------------------
  254.  
  255.     get_char - get byte from send buffer
  256.  
  257. ---------------------------------------------------------------------*/
  258.  
  259. USIGN_8 get_char()
  260. {
  261.  
  262. /* Return the byte at the current send framer pointer position.
  263. ** In the process, advance the point for next time and reduce the
  264. ** number of bytes remaining by one.
  265. */
  266. sf_len--;                    
  267. return (*sf_ptr++);
  268. }
  269.  
  270. /*LOCAL----------------------------------------------------------------
  271.  
  272.     mod_stat - modem status change interrupt service routine
  273.  
  274. ---------------------------------------------------------------------*/
  275.  
  276. void mod_stat()
  277. {
  278.  
  279. /* Read modem status register and save the value.
  280. */
  281. linestat = inp(iir_add + 4);
  282.  
  283. /* If carrier has been lost, signal the mainline that the link
  284. ** also has been lost by lowering the 'link established' flag. 
  285. */
  286. if (!(linestat & 0x80))
  287.     CLRBIT1(LINK_EST)    
  288.  
  289. }
  290.  
  291. /*LOCAL----------------------------------------------------------------
  292.  
  293.     put_char - store an LT data byte
  294.  
  295. ---------------------------------------------------------------------*/
  296.  
  297. void put_char()
  298. {
  299.  
  300. /* Ignore this byte if the amount of received data exceeds the
  301. ** negotiated maximum user data size for the LT LPDU.  This could occur
  302. ** if the frame is broken or if the correspondent is misbehaving. 
  303. */
  304. if (rf_datlen >= lcb.max_data_sz)
  305.     return;
  306.  
  307. /* Otherwise, accept this byte. 
  308. */
  309. rf_datlen++;                
  310. fcscalc (&rcv_fcs, rcv_char);
  311.  
  312. /* Now store the data byte in the receive buffer using the temporary
  313. ** insert pointer.  Advance the pointer, checking for wrap.
  314. */
  315. *rf_tiptr++ = rcv_char;            
  316. if (rf_tiptr >= rbuf + RBUF_LEN)
  317.     rf_tiptr = rbuf;
  318.  
  319. }
  320.  
  321. /*LOCAL----------------------------------------------------------------
  322.  
  323.     put_hdr